package cn.lichenfei.ffmpeg.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.lichenfei.ffmpeg.common.FFmpegUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@RestController
@RequestMapping("")
public class FileController {


    @PostMapping("upload")
    public void upload(@RequestPart("file") MultipartFile file, HttpServletResponse response) throws IOException {
        String tmpFile = FileUtil.getTmpDir() + "/" + IdUtil.fastUUID() + "." + FileUtil.extName(file.getOriginalFilename());
        IoUtil.copy(file.getInputStream(), Files.newOutputStream(Paths.get(tmpFile)));
        //
        final String output;
        String videoCover = FFmpegUtils.getVideoCover(new File(tmpFile), 5);
        IoUtil.copy(Files.newInputStream(Paths.get(videoCover)),response.getOutputStream());
    }
}
